package com.tc.es.config;

import com.tc.es.utils.ElasticSearchUtil;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

import java.net.URI;

@ConfigurationProperties(prefix = "spring.elasticsearch.rest")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration{

    private String uris;
    //重写父类方法
    @Override
    public RestHighLevelClient elasticsearchClient() {

        URI uri = ElasticSearchUtil.getIP(URI.create(uris));

        RestClientBuilder builder = RestClient.builder(new HttpHost(uri.getHost(), uri.getPort()));
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}
